package com.sywb.chuangyebao.view.dialog;

import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sywb.chuangyebao.R;
import com.umeng.analytics.MobclickAgent;

import java.io.Serializable;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

public class AppUpgradeDialog extends DialogFragment implements View.OnClickListener {

    @BindView(R.id.dialog_title)
    TextView dialogTitle;
    @BindView(R.id.title_layout)
    LinearLayout titleLayout;
    @BindView(R.id.dialog_content_image)
    ImageView dialogContentImage;
    @BindView(R.id.dialog_content_text)
    TextView dialogContentText;
    @BindView(R.id.dialog_content_tips)
    TextView dialogContentTips;
    @BindView(R.id.dialog_content_text_layout)
    LinearLayout dialogContentTextLayout;
    @BindView(R.id.dialog_content)
    FrameLayout dialogContent;
    @BindView(R.id.dialog_button_left)
    TextView dialogButtonLeft;
    @BindView(R.id.dialog_button_right)
    TextView dialogButtonRight;
    @BindView(R.id.dialog_layout)
    LinearLayout dialogLayout;

    private String title;
    private String content;
    private boolean isForceUpdate;
    private String apkUrl;

    private OnItemListener onItemListener;

    public static AppUpgradeDialog newInstance(Object... pramars) {
        AppUpgradeDialog dialogFragment = new AppUpgradeDialog();
        Bundle bundle = new Bundle();
        if (pramars != null && pramars.length > 0) {
            for (int i = 0; i < pramars.length; i++) {
                bundle.putSerializable("p" + i, (Serializable) pramars[i]);
            }
        }
        dialogFragment.setArguments(bundle);
        return dialogFragment;
    }

    private Unbinder unbinder;

    protected boolean isOnClick = true;
    protected List<Integer> noLinitClicks;
    protected long clickSleepTime = 300;
    protected long oldClickTime;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setStyle(DialogFragment.STYLE_NORMAL, R.style.common_dialog_activity);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        Dialog dialog = getDialog();
        if (dialog != null) {
            // dialog.getWindow().setDimAmount(0.0F);
            dialog.getWindow().setGravity(Gravity.CENTER);
            dialog.getWindow().setWindowAnimations(R.style.anim_bottom_up_style);
            // dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
            // dialog.setCanceledOnTouchOutside(true);
            // dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

            // WindowManager.LayoutParams params = getDialog().getWindow().getAttributes();
            // params.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
            // params.windowAnimations = R.style.bottomSheet_animation;
            // getDialog().getWindow().setAttributes(params);

            // 设置宽度顶满屏幕,无左右留白
            // DisplayMetrics dm = new DisplayMetrics();
            // getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            // getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
        }

        View view = inflater.inflate(R.layout.dialog_hint, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        if (savedInstanceState == null) {
            title = getArguments().getString("p0");
            content = getArguments().getString("p1");
            isForceUpdate = getArguments().getBoolean("p2", false);
            apkUrl = getArguments().getString("p3");
        } else {
            title = savedInstanceState.getString("p0");
            content = savedInstanceState.getString("p1");
            isForceUpdate = savedInstanceState.getBoolean("p2", false);
            apkUrl = savedInstanceState.getString("p3");
        }
        dialogTitle.setText(title);
        dialogContentText.setText(content);

        dialogButtonLeft.setText(isForceUpdate ? "退出APP" : "暂不更新");
        dialogButtonRight.setText("立即更新");

        getDialog().setCancelable(false);
        getDialog().setCanceledOnTouchOutside(false);
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {
                    return true;
                }
                return false;
            }
        });
    }

    @OnClick({R.id.dialog_button_left, R.id.dialog_button_right})
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.dialog_button_left:
                if (onItemListener != null) {
                    onItemListener.onResult(1, isForceUpdate, apkUrl);
                }
                break;
            case R.id.dialog_button_right:
                if (onItemListener != null) {
                    onItemListener.onResult(2, isForceUpdate, apkUrl);
                }
                break;
        }
        exit();
    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        outState.putString("p0", title);
        outState.putString("p1", content);
        outState.putBoolean("p2", isForceUpdate);
        outState.putString("p3", apkUrl);
        super.onSaveInstanceState(outState);
    }

    public interface OnItemListener {
        void onResult(int postion, boolean isForceUpdate, String url);
    }

    public void setOnItemListener(OnItemListener onItemListener) {
        this.onItemListener = onItemListener;
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getName());
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getName());
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        if (unbinder != null) {
            unbinder.unbind();
        }
    }

    /**
     * 是否可以继续执行点击时间
     */
    public boolean isCanClick(View view) {
        if (isOnClick) {
            int newClickView = view.getId();
            if (noLinitClicks != null && noLinitClicks.size() > 0) {
                for (int viewId : noLinitClicks) {
                    if (newClickView == viewId) {
                        return true;
                    }
                }
            }
            long newClickTime = System.currentTimeMillis();
            if ((newClickTime - oldClickTime) < clickSleepTime) {
                return false;
            } else {
                oldClickTime = newClickTime;
            }
            return true;
        }
        return false;
    }

    /**
     * 批量设置控件点击时间
     */
    protected void setOnClick(View... views) {
        for (View view : views) {
            view.setOnClickListener(this);
        }
    }

    public void exit() {
        dismiss();
    }

}
